package com.woniu.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.project.core.domain.IdentificationMap;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dto.productbigtype.ProductBigTypeSelectDto;
import com.woniu.project.params.productbigtype.ProductBigTypeAddParam;
import com.woniu.project.params.productbigtype.ProductBigTypeSelectParam;
import com.woniu.project.params.productbigtype.ProductBigTypeUpdateParam;
import com.woniu.project.service.IIdentificationMapService;
import com.woniu.project.service.IProductBigTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/productBigType")
@Api(tags="大类管理")
public class ProductBigTypeController {
    @Autowired
    private IProductBigTypeService productBigTypeService;

    @Autowired
    private IIdentificationMapService identificationMapService;
    @ApiOperation(value="大类查询",notes = "参数均选填")
    //根据是否有id和name进行不同的分页查询
    @GetMapping("/select")
    public ResultDto select(@ModelAttribute ProductBigTypeSelectParam productBigTypeSelectParam) {

        try {
            IPage<ProductBigTypeSelectDto> productBigTypeIpage = productBigTypeService.select(productBigTypeSelectParam);
            return ResultDto.success(200, "查询成功", productBigTypeIpage);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品大类查询系统异常");
        }

    }
    @ApiOperation(value="根据商品大类id查询鉴定图定义",notes = "id必填")
    //根据商品大类id查询鉴定图定义
    @GetMapping("/selectidentificationMap/{id}")
    public ResultDto select(@PathVariable Integer id) {

        try {
            List<IdentificationMap> list= identificationMapService.getByProductBigType(id);
            return ResultDto.success(200, "查询成功", list);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "鉴定图定义查询系统异常");
        }

    }
    @ApiOperation(value="新增商品大类",notes = "id不填,其他参数均必填")
    //新增商品大类
    @PostMapping("/addBigType")
    public ResultDto addBigType(@Valid @RequestBody ProductBigTypeAddParam productBigTypeAddParam) {
        try {
            int result = productBigTypeService.addBigType(productBigTypeAddParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "新增商品大类成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品大类新增系统异常");
        }
    }
    @ApiOperation(value="修改商品大类",notes = "参数全必填")
    //修改商品大类
    @PutMapping("/updateBigType")
    public ResultDto updateBigType(@Valid @RequestBody ProductBigTypeUpdateParam productBigTypeUpdateParam) {
        try {
            int result = productBigTypeService.updateBigType(productBigTypeUpdateParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "修改商品大类成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品大类修改系统异常");
        }
    }
    @ApiOperation(value="根据id删除大类",notes = "id必填")
    //根据id删除大类
    @DeleteMapping("/deleteById/{id}")
    public ResultDto deleteById(@PathVariable Integer id) {
        try {
            productBigTypeService.deleteById(id);
            return ResultDto.success(200, "删除商品大类成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品大类删除系统异常");
        }
    }

}

